package de.unibielefeld.msp.zusatzstoffe;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import de.unibielefeld.msp.zusatzstoffe.utility.Constants;
import de.unibielefeld.msp.zusatzstoffe.utility.Ingredient;

public class FoodItem extends Activity {
    private Bundle bundle;
    private SQLHelper data;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fooditem);

        bundle = getIntent().getExtras();
        TextView titleView = (TextView) findViewById(R.id.fooditemTitleTextview);
        if (bundle.containsKey("NAME")) {
            titleView.setText(bundle.getString("NAME"));
        }
        showIngredientList();
    }

    private void showIngredientList() {
        ArrayList<Ingredient> ingredientsList = new ArrayList<Ingredient>();
        if (bundle.containsKey("INGREDIENTS")) {
            String[] ingredients = bundle.getString("INGREDIENTS").split(",");
            int allergenlevel = Constants.NONALLERGENIC;
            for (String ingredientName : ingredients) {
                int dangergrade = getDangerGrade(ingredientName.trim());
                ingredientsList.add(new Ingredient(ingredientName.trim(),
                        dangergrade));
                if (dangergrade == Constants.HIGHLYALLERGENIC) {
                    allergenlevel = Constants.HIGHLYALLERGENIC;
                } else if (dangergrade == Constants.ALLERGENIC
                        && allergenlevel != Constants.HIGHLYALLERGENIC) {
                    allergenlevel = Constants.ALLERGENIC;
                }
            }
            TextView indicator = (TextView) findViewById(R.id.indicator);
            switch (allergenlevel) {
                case Constants.NONALLERGENIC:
                    indicator.setBackgroundColor(getResources().getColor(
                            R.color.nonallergenic));
                    break;
                case Constants.ALLERGENIC:
                    indicator.setBackgroundColor(getResources().getColor(
                            R.color.allergenic));
                    break;
                case Constants.HIGHLYALLERGENIC:
                    indicator.setBackgroundColor(getResources().getColor(
                            R.color.highlyallergenic));
                    break;
                default:
                    break;
            }
        }
        ListView ingredientsView = (ListView) findViewById(R.id.fooditemIngredientsList);
        IngredientAdapter adapter = new IngredientAdapter(this,
                R.layout.ingredient, ingredientsList);
        ingredientsView.setAdapter(adapter);
    }

    private int getDangerGrade(String ingredientName) {
        data = new SQLHelper(this);
        Cursor dbinf = getData(ingredientName);
        if (dbinf.moveToFirst()) {
            String dbresult = dbinf.getString(dbinf
                    .getColumnIndex(SQLHelper.GRADE));
            if (dbresult.equals("allergen")) return Constants.ALLERGENIC;
            if (dbresult.equals("hochallergen"))
                return Constants.HIGHLYALLERGENIC;
        }
        return Constants.NONALLERGENIC;
    }

    private Cursor getData(String ingredientName) {
        SQLiteDatabase db = data.getReadableDatabase();
        Cursor cursor = db.query(SQLHelper.TABLE,
                new String[] { SQLHelper.GRADE }, SQLHelper.NAME + " like ?",
                new String[] { ingredientName }, null, null, null);
        startManagingCursor(cursor);
        return cursor;
    }

    private class IngredientAdapter extends ArrayAdapter<Ingredient> {
        private List<Ingredient> ingredients;

        public IngredientAdapter(Context context, int textViewResourceId,
                List<Ingredient> ingredients) {
            super(context, textViewResourceId, ingredients);
            this.ingredients = ingredients;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.ingredient, null);
            }
            if (ingredients.size() != 0) {
                Ingredient i = ingredients.get(position);
                if (i != null) {
                    TextView tt = (TextView) v
                            .findViewById(R.id.ingredientName);
                    TextView bt = (TextView) v
                            .findViewById(R.id.ingredientAllergenLevel);
                    tt.setText(i.getIngredientName());

                    Resources res = getResources();

                    switch (i.getAllergenLevel()) {
                        case Constants.ALLERGENIC:
                            bt.setBackgroundColor(res
                                    .getColor(R.color.allergenic));
                            bt.setText(res.getString(R.string.allergenic));
                            break;
                        case Constants.HIGHLYALLERGENIC:
                            bt.setBackgroundColor(res
                                    .getColor(R.color.highlyallergenic));
                            bt.setText(res.getString(R.string.highlyallergenic));
                            break;
                        case Constants.NONALLERGENIC:
                            bt.setBackgroundColor(res
                                    .getColor(R.color.nonallergenic));
                            bt.setText(res.getString(R.string.nonallergenic));
                        default:
                            break;
                    }
                }
            }
            return v;
        }
    }
}